<template >
  <div class="header-background">
      <div class="login">
        <el-form ref="loginForm"  :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">进销存超市管理系统</h3>
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
            </el-input>
          </el-form-item>


          <el-form-item style="width:100%;">
            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>


import {login} from "@/api/login";


export default {
  name: 'loginView', // 修改组件名为多个单词
  data() {
    return {

      falg: true,
      register: '',
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
      },

    };
  },
  mounted() {
    document.querySelector('body').setAttribute('style', "background-image: url("+require("@/assets/login/login-background.png")+");background-size: cover;" +
        "background-repeat: no-repeat")
  },
  beforeDestroy() {
    document.querySelector('body').removeAttribute('style')
  },

  methods: {
    submitForm(formName) {
      console.log("123")
      let that = this
      that.$refs[formName].validate((valid) => {
        // console.log(that.loginForm)
        if (valid) {
          login(that.loginForm).then((res) => {
            console.log(res)
            if (res.code == 200) {
              localStorage.setItem("token", res.data.token)
              localStorage.setItem("username", res.data.username)
              that.$message.success(res.mess)
              this.$router.push('/index')
            } else {
              that.$message.error(res.mess)
            }
          })
        }
      });
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">


.login {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 12%;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  height: 300px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
